﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace BenhVien
{
    public partial class fmAddUser : Form
    {
        private Connection cons = new Connection();
        private DataTable dt = new DataTable();
        private DataTable dtNhanVien = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();

        public fmAddUser()
        {
            InitializeComponent();
        }

        private void fmAddUser_Load(object sender, EventArgs e)
        {
            this.cbMaNV.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            cons.Connect();
            getData();
          
        }
        private void getData()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from DangNhap";
            da.SelectCommand = command;
            da.Fill(dt);

            command.CommandText = "Select MaNV from NhanVien";
            da.SelectCommand = command;
            da.Fill(dtNhanVien);
            cbMaNV.DataSource = dtNhanVien;
            cbMaNV.DisplayMember = "MaNV";
            cbMaNV.ValueMember = "MaNV";
            cbMaNV.SelectedValue = "MaNV";
        }
        private void btAdd_Click(object sender, EventArgs e)
        {
            if (tbName.Text != "")
            {
                if (!checkStatus(tbName.Text, cbMaNV.Text))
                {
                    DataRow row = dt.NewRow();
                    row["Name"] = tbName.Text;
                    row["Pass"] = tbPass.Text;
                    row["MaNV"] = cbMaNV.SelectedValue;
                    DataTable dtCheck = new DataTable();
                    SqlCommand commandCheck = new SqlCommand();
                    commandCheck.Connection = cons.con;
                    commandCheck.CommandType = CommandType.Text;
                    commandCheck.CommandText = "Select Vitri from NhanVien Where MaNV=\'" + cbMaNV.Text + "\'";
                    da.SelectCommand = commandCheck;
                    da.Fill(dtCheck);
                    if (dtCheck.Rows[0][0].ToString() != "YT")
                    {
                        dt.Rows.Add(row);

                        SqlCommand commandInsert = new SqlCommand();
                        commandInsert.Connection = cons.con;
                        commandInsert.CommandType = CommandType.Text;
                        commandInsert.CommandText = @"Insert DangNhap (Name, Pass, MaNV)
                                        Values (@Name, @Pass, @MaNV)";
                        commandInsert.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
                        commandInsert.Parameters.Add("@Pass", SqlDbType.NVarChar, 50, "Pass");
                        commandInsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                        da.InsertCommand = commandInsert;
                        da.Update(dt);
                        //dataGridView1.DataSource = dt;
                        MessageBox.Show("Đã thêm tài khoản thành công", "Thêm tài khoản", MessageBoxButtons.OK);
                    }
                    else
                    {
                        if (MessageBox.Show("Mã Nhân Viên này là của một Y Tá(Y Tá không được cấp User). Bạn phải chọn mã nhân viên khác", "Add User", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            this.Close();
                            FormChinh _fmMain = new FormChinh();
                            _fmMain.Show();
                        }
                    }

                }
                else
                {
                    MessageBox.Show("Tài khoản đã tồn tại hoặc mã nhân viên được chọn đã có tài khoản", "Thêm tài khoản", MessageBoxButtons.OK);
                }
            }
            else
            {
                if (MessageBox.Show("Tên User không thể để trống.", "Add User", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    this.Close();
                    FormChinh _fmMain = new FormChinh();
                    _fmMain.Show();
                }
            }
                
           
            
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            //FormChinh _fmMain = new FormChinh();
            //_fmMain.Show();

        }
        private bool checkStatus(String User,String Ma)
        {
            bool check = false;
            DataTable dtCheck = new DataTable();
            dtCheck.Clear();
            SqlDataAdapter daCheck = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select Name from DangNhap Where (Name='" + User + "' or MaNV='"+Ma+"')";
            daCheck.SelectCommand = command;
            daCheck.Fill(dtCheck);
            if (dtCheck.Rows.Count > 0)
            {
                check = true;
            }
            return check;
        }
       
        private void ckUnHine_CheckedChanged(object sender, EventArgs e)
        {
            if (ckUnHine.Checked)
            {
                tbPass.UseSystemPasswordChar = false;
            }
            else
            {
                tbPass.UseSystemPasswordChar = true;
            }
        }

        private void cbMaNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dtHelp = new DataTable();
            SqlCommand commandHelp = new SqlCommand();
            commandHelp.Connection = cons.con;
            commandHelp.CommandType = CommandType.Text;
            commandHelp.CommandText = "Select * from NhanVien where MaNV=\'"+cbMaNV.Text+"\'";
            da.SelectCommand = commandHelp;
            da.Fill(dtHelp);
            dtGVUser.DataSource = dtHelp;
            if (checkStatus("", cbMaNV.Text)) lbhelp.Text = "Đã có User";
            else lbhelp.Text = "Chưa có User";
        }

       

    }
}
